Conditions | 1 |
Paths | 49 |
Total Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
64 | inji.onLoad(function () { |
||
65 | |||
66 | //plugin bootstrap minus and plus |
||
67 | //http://jsfiddle.net/laelitenetwork/puJ6G/ |
||
68 | $('body').on('click', '.btn-number', function (e) { |
||
69 | e.preventDefault(); |
||
70 | |||
71 | var fieldName = $(this).data('field'); |
||
72 | var type = $(this).data('type'); |
||
73 | var input = $("input[name='" + fieldName + "']"); |
||
74 | var currentVal = parseFloat(input.val()); |
||
75 | if (!isNaN(currentVal)) { |
||
76 | if (type == 'minus') { |
||
77 | |||
78 | if (currentVal > input.attr('min')) { |
||
79 | input.val(currentVal - 1).change(); |
||
80 | } |
||
81 | if (parseFloat(input.val()) == input.attr('min')) { |
||
82 | $(this).attr('disabled', true); |
||
83 | } |
||
84 | |||
85 | } else if (type == 'plus') { |
||
86 | |||
87 | if (currentVal < input.attr('max')) { |
||
88 | input.val(currentVal + 1).change(); |
||
89 | } |
||
90 | if (parseFloat(input.val()) == input.attr('max')) { |
||
91 | $(this).attr('disabled', true); |
||
92 | } |
||
93 | |||
94 | } |
||
95 | } else { |
||
96 | input.val(0); |
||
97 | } |
||
98 | }); |
||
99 | $('body').on('focusin', '.input-number', function () { |
||
100 | $(this).data('oldValue', $(this).val()); |
||
101 | }); |
||
102 | $('body').on('change', '.input-number', function () { |
||
103 | |||
104 | var minValue = parseFloat($(this).attr('min')); |
||
105 | var maxValue = parseFloat($(this).attr('max')); |
||
106 | var valueCurrent = parseFloat($(this).val()); |
||
107 | |||
108 | var name = $(this).attr('name'); |
||
109 | if (valueCurrent >= minValue) { |
||
110 | $(".btn-number[data-type='minus'][data-field='" + name + "']").removeAttr('disabled') |
||
111 | } else { |
||
112 | alert('Нельзя заказать меньше ' + minValue); |
||
113 | $(this).val($(this).data('oldValue')); |
||
114 | } |
||
115 | if (valueCurrent <= maxValue) { |
||
116 | $(".btn-number[data-type='plus'][data-field='" + name + "']").removeAttr('disabled') |
||
117 | } else { |
||
118 | alert('Извините, но больше нету'); |
||
119 | $(this).val($(this).data('oldValue')); |
||
120 | } |
||
121 | |||
122 | |||
123 | }); |
||
124 | |||
125 | $('body').on('keydown', ".input-number", function (e) { |
||
126 | // Allow: backspace, delete, tab, escape, enter and . |
||
127 | if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 || |
||
128 | // Allow: Ctrl+A |
||
129 | (e.keyCode == 65 && e.ctrlKey === true) || |
||
130 | // Allow: home, end, left, right |
||
131 | (e.keyCode >= 35 && e.keyCode <= 39)) { |
||
132 | // let it happen, don't do anything |
||
133 | return; |
||
134 | } |
||
135 | // Ensure that it is a number and stop the keypress |
||
136 | if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) { |
||
137 | e.preventDefault(); |
||
138 | } |
||
139 | }); |
||
140 | |||
141 | }) |
||
142 |